//
//  BySetVC.m
//  caipiao_ios
//
//  Created by mc on 2018/7/9.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "BySetVC.h"

@interface BySetVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation BySetVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addLeftBackBarButtonItem];
    self.title = @"设置";
    [self initViews];
    // Do any additional setup after loading the view.
}

- (void)initViews{
    self.view.backgroundColor = BY_BACKGROUND_COLOR;
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = BY_BACKGROUND_COLOR;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        if (@available(ios 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(0);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(0);
        } else {
            make.top.equalTo(self.view).offset(0);
            make.bottom.equalTo(self.view).offset(0);
        }
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.scrollEnabled = NO;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        static NSString *cellIdentifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            
            UILabel *line = [[UILabel alloc] init];
            [cell addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(cell).offset(0);
                make.right.equalTo(cell).offset(0);
                make.bottom.equalTo(cell).offset(0);
                make.height.mas_equalTo(@1);
            }];
            line.backgroundColor = BY_LINE_COLOR;
            
    
        }
        
        for (UIView *view in cell.contentView.subviews) {
            [view removeFromSuperview];
        }
        
        UILabel *titleLabel = [[UILabel alloc] init];
        [cell.contentView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(cell.contentView).offset(10);
            make.centerY.equalTo(cell.contentView).offset(0);
            make.size.mas_equalTo(CGSizeMake(120, 20));
        }];
        
        titleLabel.font = FONT(14);
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.text = @"通知中心";
        
        UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 60, 13, 45, 25)];
        [cell.contentView addSubview:switchView];
        switchView.onTintColor = BY_THEME_COLOR;
        switchView.on = BY_USER.app_push.integerValue == 1;
        [switchView addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventTouchUpInside];
        
        return cell;
        
    }else{
        
        
        static NSString *cellIdentifier = @"cell111";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            
            UILabel *line = [[UILabel alloc] init];
            [cell addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(cell).offset(0);
                make.right.equalTo(cell).offset(0);
                make.bottom.equalTo(cell).offset(0);
                make.height.mas_equalTo(@1);
            }];
            line.backgroundColor = BY_LINE_COLOR;
            
        }
        for (UIView *view in cell.contentView.subviews) {
            [view removeFromSuperview];
        }
        
        if (indexPath.section == 1) {
            
            UILabel *titleLabel = [[UILabel alloc] init];
            [cell.contentView addSubview:titleLabel];
            [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(cell.contentView).offset(10);
                make.centerY.equalTo(cell.contentView).offset(0);
                make.size.mas_equalTo(CGSizeMake(120, 20));
            }];
            
            titleLabel.font = FONT(14);
            titleLabel.textColor = [UIColor blackColor];
            titleLabel.text = @"清除缓存";
            
            UILabel *valueLabel = [[UILabel alloc] init];
            [cell.contentView addSubview:valueLabel];
            [valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(cell.contentView).offset(-10);
                make.centerY.equalTo(cell.contentView).offset(0);
                make.size.mas_equalTo(CGSizeMake(120, 20));
            }];
            
            valueLabel.font = FONT(14);
            valueLabel.textColor = [UIColor blackColor];
            valueLabel.text = [self getCacheSize];
            valueLabel.textAlignment = NSTextAlignmentRight;
            
        }else{
            
            UILabel *titleLabel = [[UILabel alloc] init];
            [cell.contentView addSubview:titleLabel];
            [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(cell.contentView).offset(0);
                make.centerY.equalTo(cell.contentView).offset(0);
                make.size.mas_equalTo(CGSizeMake(120, 20));
            }];
            
            titleLabel.font = FONT(14);
            titleLabel.textColor = BY_THEME_COLOR;
            titleLabel.textAlignment = NSTextAlignmentCenter;
            titleLabel.text = @"退出登录";
        }
        return cell;
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 48;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 40;
    }else if (section == 1){
        return 0.01;
    }
    return 20;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        
        UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
        UILabel *titleLabel = [[UILabel alloc] init];
        [header addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(header).offset(10);
            make.centerY.equalTo(header).offset(0);
            make.size.mas_equalTo(CGSizeMake(120, 20));
        }];
        
        titleLabel.font = FONT(12);
        titleLabel.textColor = [UIColor grayColor];
        titleLabel.text = @"开启APP通知";
        
        return header;
        
    }else if (section == 1){
        return [UIView new];
    }else{
        return [UIView new];
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 2) {
        
        [ByCacheUser setUserInfo:nil];
        [self leftItemsAction:0];
        
    //清除缓存
    }else if (indexPath.section == 1){
        
        NSString *message = [NSString stringWithFormat:@"缓存大小：%@",[self getCacheSize]];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定清除缓存" message:message preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
                [self.tableView reloadData];
            }];
        }];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        
        [alert addAction:okAction];
        [alert addAction:cancelAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

#pragma mark - 计算缓存
-(NSString *)getCacheSize
{
    //计算缓存大小
    NSUInteger fileSize = [[SDImageCache sharedImageCache] getSize];
    //转换为兆
    return [NSString stringWithFormat:@"%.2fM",fileSize / 1024.0 / 1024.0];
}


- (void)switchAction:(UISwitch *)sch{

    ByModelUser *user = BY_USER;
    if (sch.isOn) {
        user.app_push = @"1";
    }else{
        user.app_push = @"0";
    }
    [ByCacheUser setUserInfo:user];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
